/**
 * 
 */
package com.goodedeeds.webapp.controller;

import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.validation.ValidationError;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author ravi
 *
 */
public class BaseAction implements ActionBean {

	protected static final String JS_ROOT = "js";
	protected static final String CSS_ROOT = "css";
	protected static final String JSP_ROOT = "/WEB-INF/jsp";
	protected static final String BASE_HTML_PAGE = JSP_ROOT + "/include/base-html.jsp";
	
	private ActionBeanContext context;
	
	private List<String>cssFiles = new ArrayList<String>();
	private List<String>jsFiles = new ArrayList<String>();
	
	/* (non-Javadoc)
	 * @see net.sourceforge.stripes.action.ActionBean#getContext()
	 */
	public ActionBeanContext getContext() {
		return context;
	}

	/* (non-Javadoc)
	 * @see net.sourceforge.stripes.action.ActionBean#setContext(net.sourceforge.stripes.action.ActionBeanContext)
	 */
	public void setContext(ActionBeanContext context) {
		this.context = context;
	}

    @DefaultHandler
	public final Resolution showPage(){
		return new ForwardResolution(BASE_HTML_PAGE);
	}

	public String getBodyContent(){
		return null;
	}
	
	public String getHeaderInclude(){
		return null;
	}

    public String getHeaderContent(){
        return null;
    }

	public final List<String> getCssFiles(){
		return cssFiles;
	}
	
	public final List<String> getJsFiles(){
		return jsFiles;
	}
	
	
	protected final void addCssFiles(Collection<String> cssFiles){
		if(cssFiles != null){
			for(String cssFile : cssFiles){
				addCssFile(cssFile);	
			}
		}		
	}
	
	protected final void addCssFiles(String[] cssFiles){
		if(cssFiles != null){
			for(String cssFile : cssFiles){
				addCssFile(cssFile);	
			}
		}
	}
		
	protected final void addCssFile(String cssFile){
		if(cssFile != null && !this.cssFiles.contains(cssFile)){
			cssFiles.add(cssFile);
		}
	}
	
	protected final void addJsFiles(Collection<String> jsFiles){
		if(jsFiles != null){
			for(String jsFile : jsFiles){
				addJsFile(jsFile);	
			}
		}		
	}
	
	protected final void addJsFiles(String[] jsFiles){
		if(jsFiles != null){
			for(String jsFile : jsFiles){
				addJsFile(jsFile);	
			}
		}
	}
		
	protected final void addJsFile(String jsFile){
		if(jsFile != null && !this.jsFiles.contains(jsFile)){
			jsFiles.add(jsFile);
		}
	}

    private static final String TITLE = "GoodeDeeds.com";

    public String getTitle() {
        return TITLE;
    }


    protected void addValidationError(String field, ValidationError validationError){
        getContext().getValidationErrors().add(field, validationError);
    }

    protected void addGlobalError(ValidationError globalError){
        getContext().getValidationErrors().addGlobalError(globalError);
    }


    protected final HttpServletRequest getRequest(){
        return getContext().getRequest();
    }

    protected final HttpServletResponse getResponse(){
        return getContext().getResponse();
    }

    protected final String getRequestParameter(String paramName){
        return getRequest().getParameter(paramName);
    }
}
